package org.groovymud.scripting.utils;

import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import org.groovymud.config.MudConfig;
import org.groovymud.object.registry.MudObjectAttendant;
import org.groovymud.object.registry.Registry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.script.Bindings;
import javax.script.SimpleBindings;
import java.util.HashMap;
import java.util.Map;

public class BindingDelegate implements ApplicationContextAware {
    private final MudConfig config;
    private ApplicationContext applicationContext;

    public BindingDelegate(MudConfig config){
        this.config = config;
    }
    public Bindings createJSR223ScriptEngineBindings() {
        return createJSR223ScriptEngineBindings(new HashMap<String, Object>());
    }

    public Bindings createJSR223ScriptEngineBindings(Map<String, Object> bindings) {
        return new SimpleBindings(bindScriptVariables(bindings));
    }

    private Map<String, Object> bindScriptVariables(Map<String, Object> bindings) {
        bindings.put("mudSpace", getMudSpace());
        bindings.put("store", getStore());
        bindings.put("scriptSpace", getScriptSpace());
        bindings.put("context", applicationContext);
        bindings.put("registry", applicationContext.getBean(Registry.class));
        bindings.put("attendant", applicationContext.getBean(MudObjectAttendant.class));
        bindings.put("groovyScriptEngine", applicationContext.getBean(GroovyScriptEngine.class));
        return bindings;
    }

    public Binding createGroovyScriptEngineBinding() {
        return createGroovyScriptEngineBinding(new HashMap<String, Object>());
    }

    public Binding createGroovyScriptEngineBinding(Map<String, Object> bindings) {
        return new Binding(bindScriptVariables(bindings));
    }


    /**
     * @return the mudSpace
     */
    public String getMudSpace() {
        return config.getMudSpace();
    }

    /**
     * @param mudSpace the mudSpace to set
     */
    public void setMudSpace(String mudSpace) {
        this.config.setMudSpace(mudSpace);
    }

    /**
     * @return the scriptSpace
     */
    public String getScriptSpace() {
        return config.getScriptSpace();
    }

    /**
     * @param scriptSpace the scriptSpace to set
     */
    public void setScriptSpace(String scriptSpace) {
        this.config.setScriptSpace(scriptSpace);
    }

    /**
     * @return the store
     */
    public String getStore() {
        return config.getStore();
    }

    /**
     * @param store the store to set
     */
    public void setStore(String store) {
        this.config.setStore(store);
    }


    /**
     * @return the config
     */
    public MudConfig getConfig() {
        return config;
    }
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
